const express = require('express');
const http = require('http');
const WebSocket = require('ws');

const app = express();

// 创建 HTTP server
const server = http.createServer(app);

// 创建 WebSocket 服务器实例，挂载到 HTTP server 上
const wss = new WebSocket.Server({ server });

wss.on('connection', (ws) => {
    console.log('Client connected');

    // 模拟发送错误信息
    setTimeout(() => {
        ws.send('这是一个模拟的错误消息！');
    }, 2000); // 2 秒后发送错误

    // 模拟错误消失
    setTimeout(() => {
        ws.send('noError');
    }, 10000); // 10 秒后发送 noError，表示错误消失
});

// 启动 HTTP 和 WebSocket 服务器
server.listen(8080, () => {
    console.log('Server started on http://localhost:8080');
});
